package fr.univ.lille1.android.auth;

import fr.univ.lille1.android.domain.User;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SessionStore {
	private static final String KEY_ID = "tagthis_user_id";
	private static final String KEY_NAME = "tagthis_user_name";
	private static final String KEY_MAIL = "tagthis_user_mail";
	private static final String KEY_TOKEN = "tagthis_token";
	private static final String KEY_TOKEN_TIMESTAMP = "tagthis_timestamp";
	private static final String KEY_SHARED_PREFERENCE = "tagthis_session";

	/**
	 * Check is a user is already logged. Look in the {@link SharedPreferences}
	 * to see if there are token and its timestamp.
	 * 
	 * @param context
	 *            Context of the application.
	 * @return true when the user is logged.
	 */
	public static boolean isLogged(Context context) {
		final SharedPreferences session = context.getSharedPreferences(
				KEY_SHARED_PREFERENCE, Context.MODE_PRIVATE);
		final String token = session.getString(KEY_TOKEN, null);
		final long timestamp = session.getLong(KEY_TOKEN_TIMESTAMP, 0);
		return token != null && timestamp != 0;
	}

	/**
	 * Check if the connection of the user is valid. Look in the
	 * {@link SharedPreferences} to see if the current timestamp is lower than
	 * the timestamp of the token saved.
	 * 
	 * @param context
	 *            Context of the application.
	 * @return true when the connection is valid.
	 */
	public static boolean isConnectionValid(Context context) {
		final SharedPreferences session = context.getSharedPreferences(
				KEY_SHARED_PREFERENCE, Context.MODE_PRIVATE);
		final long timestamp = session.getLong(KEY_TOKEN_TIMESTAMP, 0);
		return timestamp > System.currentTimeMillis();
	}

	/**
	 * Save token and its timestamp. Save them in {@link SharedPreferences} to
	 * restore them another time.
	 * 
	 * @param context
	 *            Context of the application.
	 * @param sessionKey
	 *            Token of the current session.
	 * @param timestamp
	 *            Timestamp of the token.
	 * @return true when informations is well saved.
	 */
	public static boolean saveToken(Context context, String sessionKey,
			long timestamp) {
		final Editor editor = context.getSharedPreferences(
				KEY_SHARED_PREFERENCE, Context.MODE_PRIVATE).edit();
		editor.putString(KEY_TOKEN, sessionKey);
		editor.putLong(KEY_TOKEN_TIMESTAMP, timestamp);
		return editor.commit();
	}

	/**
	 * Save user logged. Save it in {@link SharedPreferences} to restore it
	 * another time.
	 * 
	 * @param context
	 *            Context of the application.
	 * @param userLogged
	 *            User logged.
	 * @return true when informations is well saved.
	 */
	public static boolean saveUser(Context context, User userLogged) {
		final Editor editor = context.getSharedPreferences(
				KEY_SHARED_PREFERENCE, Context.MODE_PRIVATE).edit();
		editor.putInt(KEY_ID, userLogged.getId());
		editor.putString(KEY_NAME, userLogged.getName());
		editor.putString(KEY_MAIL, userLogged.getMail());
		return editor.commit();
	}

	/**
	 * Restore a user saved in {@link SharedPreferences} during his
	 * authentication.
	 * 
	 * @param context
	 *            Context of the application.
	 * @return User logged.
	 */
	public static User restoreUser(Context context) {
		final SharedPreferences session = context.getSharedPreferences(
				KEY_SHARED_PREFERENCE, Context.MODE_PRIVATE);
		final int id = session.getInt(KEY_ID, 0);
		final String name = session.getString(KEY_NAME, null);
		final String email = session.getString(KEY_MAIL, null);
		return new User(id, name, null, email);
	}

	/**
	 * Flush all data in {@link SharedPreferences}.
	 * 
	 * @param context
	 *            Context of the application.
	 */
	public static boolean flush(Context context) {
		final Editor editor = context.getSharedPreferences(
				KEY_SHARED_PREFERENCE, Context.MODE_PRIVATE).edit();
		editor.putInt(KEY_ID, -1);
		editor.putString(KEY_NAME, null);
		editor.putString(KEY_MAIL, null);
		editor.putString(KEY_TOKEN, null);
		editor.putLong(KEY_TOKEN_TIMESTAMP, -1);
		return editor.commit();
	}
}
